package org.one.afternoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.one.afternoon.common.Result;
import org.one.afternoon.domain.User;
import org.one.afternoon.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;


    @CrossOrigin // 跨域访问
    @GetMapping("/users")
    public Result<List<User>> findAllUser(){
        List<User> userList=userService.list(null);
        return Result.success(userList);
    }


    //复合查询
    @CrossOrigin
    @PostMapping("/query")
    @ResponseBody
    public Result<List<User>> queryUser(@RequestBody User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(user.getUsername()),User::getUsername,user.getUsername())
                .like(user.getAge()!=null,User::getAge,user.getAge())
                .like(StringUtils.isNotBlank(user.getSex()),User::getSex,user.getSex())
                .like(StringUtils.isNotBlank(user.getRole()),User::getRole,user.getRole());
        List<User> list = userService.list(lambdaQueryWrapper);
        Result<List<User>> result=Result.success(list);
        return result;
    }

    @CrossOrigin
    @PostMapping("/insert")
    @ResponseBody
    public Result<String> insert(@RequestBody User user) {
        boolean save = userService.save(user);
        System.out.println(user.getId());
        Result<String> result;
        if (!save){
            result = Result.error("添加用户失败");
        }else {
            result = Result.success("成功添加用户");
        }
        return result;
    }

    @CrossOrigin
    @PostMapping("/delete")
    @ResponseBody
    public Result<String> delete(@RequestParam Integer index) {
        userService.removeById(index);
        Result<String> result=Result.success("删除成功");
        return result;
    }



    @CrossOrigin
    @PostMapping("/update")
    @ResponseBody
    public Result<String> update(@RequestBody User user) {
        LambdaUpdateWrapper<User> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(StringUtils.isNotBlank(user.getUsername()),User::getUsername,user.getUsername())
                .set(user.getAge()!=null,User::getAge,user.getAge())
                .set(StringUtils.isNotBlank(user.getSex()),User::getSex,user.getSex())
                .set(StringUtils.isNotBlank(user.getRole()),User::getRole,user.getRole());
        userService.update(null,lambdaUpdateWrapper);
        Result<String> result=Result.success("修改成功");
        return result;
    }



    @CrossOrigin
    @PostMapping("/login")
    @ResponseBody
    public Result<String> login(@RequestBody User user) {
        boolean login = userService.login(user);
        if(login){
           return Result.success("登录成功");
        }else{
            return Result.error("登录失败");
        }
    }

    @CrossOrigin
    @GetMapping("/info")
    @ResponseBody
    public Result<User> info() {
        // todo: 先看是否登录，是的话 返回token和信息，否的话返回null
        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("roles","admin");
        responseData.put("name","Xm");
        responseData.put("avatar","");
        responseInfo.put("code",20000);
        responseInfo.put("msg","登录成功");
        responseInfo.put("data",responseData);
        return null;
    }



    @CrossOrigin
    @PostMapping("/logout")
    @ResponseBody
    public Result<String> logout(@RequestParam String username) {
        boolean logout = userService.logout(username);
        if(logout){
            System.out.println("登出成功");
            return Result.success("登出成功");
        }else{
            System.out.println("登出失败");
            return Result.error("登出失败");
        }
    }

    @CrossOrigin
    @GetMapping ("/online")
    public Result<Long> queryOnline() {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getOnline,1);
        long count = userService.count(lambdaQueryWrapper);
        Result<Long> result=Result.success(count);
        return result;
    }

    @CrossOrigin
    @GetMapping ("/total")
    public Result<Long> queryTotal() {
        long count = userService.count();
        Result<Long> result=Result.success(count);
        return result;
    }

    @CrossOrigin
    @GetMapping ("/tdy")
    public Result<Integer> queryTdy(@RequestParam int time) {
        Integer tdy=userService.queryTdy(time);
        Result<Integer> result=Result.success(tdy);
        return result;
    }

    @CrossOrigin
    @GetMapping ("/new")
    public Result<Integer> queryNew(@RequestParam int time) {
        Integer New=userService.queryNew(time);
        Result<Integer> result=Result.success(New);
        return result;
    }

}
